﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EYangFramework.Domain.Entitys.Entrys
{
    public abstract class Entity : IEntity
    {
        [Required]
        [Key]
        public virtual Guid ID { get; set; }
        public virtual DateTime CREATETIME { get; set; }
        public virtual string CREATEUSER { get; set; }
        public Entity()
        {
            ID = Guid.NewGuid();
            CREATETIME = DateTime.Now;
        }
        public Entity(User createUser)
        {
            ID = Guid.NewGuid();
            CREATETIME = DateTime.Now;
            CREATEUSER = createUser?.ID.ToString(); 
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            if (obj.GetType() != this.GetType())
            {
                return false;
            }
            return this.ID == (obj as Entity).ID;
        }
        public override int GetHashCode()
        {
            return this.ID.GetHashCode();
        }

        public static bool operator ==(Entity entity1, Entity entity2)
        {
            if (entity2 is null && entity1 is null)
            {
                return true;
            }
            if (entity2 is null || entity1 is null)
            {
                return false;
            }
            return entity1.ID == entity2.ID;
        }
        public static bool operator !=(Entity entity1, Entity entity2)
        {
            return !(entity1 == entity2);
        }
    }
}
